package com.sr.servlet.User;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sr.dao.UserDao;
import com.sr.entity.Book;
import com.sr.entity.User;
import com.sr.service.BookService;
import com.sr.service.UserService;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/userlogin")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码格式
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		
		//从前台注册页面得到用户输入的数据             实则：获取用户写入的信息
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		 // 将用户名称作为参数传递给后台DAO   实例化一个实体类User的对象
		User user=new User(username,password);//初始化 写入构造方法的参数 会用到
		UserService us=new UserService();
		boolean result=us.login(username, password);
		if(result) {
					System.out.println("登录成功！");
					user=us.findUserByUserName(username);
					request.getSession().setAttribute("username", username);
					request.getRequestDispatcher("findhomebook").forward(request, response);	
					//response.sendRedirect("findhomebook");//这个要跳到home 页面的servlet进行刷新	
				}else {
					//登录不成功跳转的登录页面
					request.getRequestDispatcher("login.jsp").forward(request, response);	
			}		
		}
	
}
